﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project.Models;
using Project.Database;
using Project.Database.Repository;
using Project.Implementations;

namespace Project.Controllers
{
    public class BlogController : Controller
    {
        private DatabaseRepository entities = new DatabaseRepository();

        [Authorize]
        public ActionResult List(int? catid, int? userid)
        {
            if (catid != null && userid != null)
            {
                User user = entities.GetUser((int)userid);
                return View(user.Blogs.Where(b => b.CategoryID == catid));
            }
            if (catid != null && userid == null)
            {
                return View(entities.GetBlogsByCategory((int)catid));
            }
            if (catid == null && userid != null)
            {
                return View(entities.GetUser((int)userid).Blogs);
            }
            return View(entities.GetAllBlogs());
        }

        [Authorize(Roles = "Blogger")]
        public ActionResult Show(int id = -1)
        {
            Blog blog = entities.GetBlog(entities.GetUser(User.Identity.Name), id);
            if (blog == null)
            {
                return RedirectToAction("Index", "Profile");
            }
            User user = entities.GetUser(User.Identity.Name);
            List<string> html = new List<string>();
            foreach (Advert advert in blog.Adverts)
            {
                html.Add(Utils.GenerateAdHTML(user,advert,blog));
            }
            ViewBag.AdHtml = html;
            return View(blog);
        }

        [Authorize(Roles = "Blogger")]
        public ActionResult AddBlog()
        {
            ViewBag.Categories = entities.GetCategories();
            return View();
        }

        [Authorize(Roles = "Blogger")]
        [HttpPost]
        public ActionResult AddBlog(BlogModel blogmodel)
        {
            //CHECK MODEL STATE?
            try
            {
                entities.CreateBlog(User.Identity.Name, blogmodel.Name, blogmodel.URL, blogmodel.Keywords, blogmodel.categoryID);
                return RedirectToAction("Index", "Profile");
            }
            catch (ArgumentException ae)
            {
                ModelState.AddModelError("", ae.Message);
            }
            return View();
        }

        [Authorize(Roles = "Blogger")]
        public ActionResult AddAdvert(int aid, int bid)
        {
            //CHECK MODEL STATE?
            User user = entities.GetUser(User.Identity.Name);
            Blog blog = entities.GetBlog(bid);
            Advert advert = entities.GetAdvert(aid);
            if (entities.CheckAdAdvert(user, blog, advert))
            {
                entities.AddAdvertToBlog(blog, advert);
            }
            else
            {
                ModelState.AddModelError("", "Could not add advert to blog.");
            }
            return RedirectToAction("Show", new { id = blog.ID });
        }
    }
}
